{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-17T06:29:12.306815Z",
     "start_time": "2025-01-17T06:29:12.297354Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 109
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-17T06:29:12.628066Z",
     "start_time": "2025-01-17T06:29:12.622664Z"
    }
   },
   "cell_type": "code",
   "source": [
    "index = pd.MultiIndex(levels=[['Sunday','Monday','Tuesday'],['T1','T2']],\n",
    "                      codes=[[0,0,1,1,2,2],[0,1,0,1,0,1]],\n",
    "                      names=['Date', 'Number'])\n",
    "columns = pd.MultiIndex.from_tuples([('A',),('B',),('C',),('D',)])"
   ],
   "id": "2babf5471c3a97f8",
   "outputs": [],
   "execution_count": 110
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-17T06:29:13.018482Z",
     "start_time": "2025-01-17T06:29:13.010913Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.random.randint(0,20,size=(6,4))\n",
    "print(a)"
   ],
   "id": "adaf311e9a5eb77d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10  7  9  4]\n",
      " [ 3 11  4  7]\n",
      " [ 6 19  6 10]\n",
      " [ 1  2 11 14]\n",
      " [16  6 11 19]\n",
      " [ 7  5  0 11]]\n"
     ]
    }
   ],
   "execution_count": 111
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-17T06:29:13.419651Z",
     "start_time": "2025-01-17T06:29:13.409695Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = pd.DataFrame(a,index=index,columns=columns)\n",
    "data.index.names = ['Date','Number']\n",
    "print(data)\n",
    "# print(data.index)\n",
    "# print(data.values)"
   ],
   "id": "16521331bbec3049",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                 A   B   C   D\n",
      "Date    Number                \n",
      "Sunday  T1      10   7   9   4\n",
      "        T2       3  11   4   7\n",
      "Monday  T1       6  19   6  10\n",
      "        T2       1   2  11  14\n",
      "Tuesday T1      16   6  11  19\n",
      "        T2       7   5   0  11\n"
     ]
    }
   ],
   "execution_count": 112
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-17T06:29:13.982666Z",
     "start_time": "2025-01-17T06:29:13.974790Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 列切片\n",
    "print(data['A'])\n",
    "print(data['A':'C'])"
   ],
   "id": "9a83f194a4c25134",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                 A\n",
      "Date    Number    \n",
      "Sunday  T1      10\n",
      "        T2       3\n",
      "Monday  T1       6\n",
      "        T2       1\n",
      "Tuesday T1      16\n",
      "        T2       7\n",
      "                 A   B   C\n",
      "Date    Number            \n",
      "Sunday  T1      10   7   9\n",
      "        T2       3  11   4\n",
      "Monday  T1       6  19   6\n",
      "        T2       1   2  11\n",
      "Tuesday T1      16   6  11\n",
      "        T2       7   5   0\n"
     ]
    }
   ],
   "execution_count": 113
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-17T06:29:14.985955Z",
     "start_time": "2025-01-17T06:29:14.979588Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 列+行切片\n",
    "print(data.loc['Sunday',:])\n",
    "print(data.loc['Monday','T1'])"
   ],
   "id": "155c3bc2ae5a4849",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         A   B  C  D\n",
      "Number              \n",
      "T1      10   7  9  4\n",
      "T2       3  11  4  7\n",
      "A     6\n",
      "B    19\n",
      "C     6\n",
      "D    10\n",
      "Name: (Monday, T1), dtype: int32\n"
     ]
    }
   ],
   "execution_count": 114
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-17T06:29:31.236465Z",
     "start_time": "2025-01-17T06:29:31.227946Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用IndexSlice对象\n",
    "idx = pd.IndexSlice\n",
    "print(data.loc[idx[:,'T1'],idx['A']])  # 查询横数据中，第二级索引为“T1”、列数据中，第一级索引为“A”的数据\n",
    "print(data.loc[idx[:,'T1'],idx['A':'C']]) # 查询横数据中，第二级索引为“T1”、列数据中，第一级索引为“A”到\"C\"的数据"
   ],
   "id": "c7c63c8147ea0cae",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                 A\n",
      "Date    Number    \n",
      "Sunday  T1      10\n",
      "Monday  T1       6\n",
      "Tuesday T1      16\n",
      "                 A   B   C\n",
      "Date    Number            \n",
      "Sunday  T1      10   7   9\n",
      "Monday  T1       6  19   6\n",
      "Tuesday T1      16   6  11\n"
     ]
    }
   ],
   "execution_count": 115
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-17T06:29:05.007527Z",
     "start_time": "2025-01-17T06:29:05.003033Z"
    }
   },
   "cell_type": "code",
   "source": "print(data)",
   "id": "e9b64c75ea387331",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                 A   B   C   D\n",
      "Date    Number                \n",
      "Sunday  T1       8  18  17   8\n",
      "        T2       8  14   1  10\n",
      "Monday  T1       0  17   3  13\n",
      "        T2       3   3  11  15\n",
      "Tuesday T1      14   7   9  15\n",
      "        T2       1   0  17   2\n"
     ]
    }
   ],
   "execution_count": 103
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-17T06:29:05.028018Z",
     "start_time": "2025-01-17T06:29:05.018559Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(data['Sunday':'Tuesday'])\n",
    "print(data.sort_index())\n",
    "print(data.sort_index()['Monday':'Sunday'])"
   ],
   "id": "e05fa2b941bba587",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Empty DataFrame\n",
      "Columns: []\n",
      "Index: [(Sunday, T1), (Sunday, T2), (Monday, T1), (Monday, T2), (Tuesday, T1), (Tuesday, T2)]\n",
      "                 A   B   C   D\n",
      "Date    Number                \n",
      "Monday  T1       0  17   3  13\n",
      "        T2       3   3  11  15\n",
      "Sunday  T1       8  18  17   8\n",
      "        T2       8  14   1  10\n",
      "Tuesday T1      14   7   9  15\n",
      "        T2       1   0  17   2\n",
      "Empty DataFrame\n",
      "Columns: []\n",
      "Index: [(Monday, T1), (Monday, T2), (Sunday, T1), (Sunday, T2), (Tuesday, T1), (Tuesday, T2)]\n"
     ]
    }
   ],
   "execution_count": 104
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-17T06:30:17.721305Z",
     "start_time": "2025-01-17T06:30:17.713708Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(data)\n",
    "print(\"==================================================\")\n",
    "print(data.unstack(level=1))"
   ],
   "id": "d342ccad74a74b79",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                 A   B   C   D\n",
      "Date    Number                \n",
      "Sunday  T1      10   7   9   4\n",
      "        T2       3  11   4   7\n",
      "Monday  T1       6  19   6  10\n",
      "        T2       1   2  11  14\n",
      "Tuesday T1      16   6  11  19\n",
      "        T2       7   5   0  11\n",
      "==================================================\n",
      "          A      B       C       D    \n",
      "Number   T1 T2  T1  T2  T1  T2  T1  T2\n",
      "Date                                  \n",
      "Sunday   10  3   7  11   9   4   4   7\n",
      "Monday    6  1  19   2   6  11  10  14\n",
      "Tuesday  16  7   6   5  11   0  19  11\n"
     ]
    }
   ],
   "execution_count": 120
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-17T06:29:05.045723Z",
     "start_time": "2025-01-17T06:29:05.038241Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建示例数据\n",
    "data = {\n",
    "    ('A', 'a'): [1, 2, 3],\n",
    "    ('A', 'b'): [4, 5, 6],\n",
    "    ('B', 'a'): [7, 8, 9],\n",
    "    ('B', 'b'): [10, 11, 12]\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "df.columns = pd.MultiIndex.from_tuples(df.columns)\n",
    "df.index = pd.MultiIndex.from_tuples([('X', 'x'), ('X', 'y'), ('Y', 'x')])\n",
    "print(\"原始 DataFrame:\")\n",
    "print(df)"
   ],
   "id": "57e17e8a8ae2753c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始 DataFrame:\n",
      "     A     B    \n",
      "     a  b  a   b\n",
      "X x  1  4  7  10\n",
      "  y  2  5  8  11\n",
      "Y x  3  6  9  12\n"
     ]
    }
   ],
   "execution_count": 106
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-17T06:29:05.060388Z",
     "start_time": "2025-01-17T06:29:05.052338Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.unstack(level=0))  # unstack方法是从行解到列",
   "id": "4956b9311fee6d32",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     A                   B                 \n",
      "     a         b         a          b      \n",
      "     X    Y    X    Y    X    Y     X     Y\n",
      "x  1.0  3.0  4.0  6.0  7.0  9.0  10.0  12.0\n",
      "y  2.0  NaN  5.0  NaN  8.0  NaN  11.0   NaN\n"
     ]
    }
   ],
   "execution_count": 107
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-17T06:29:05.085273Z",
     "start_time": "2025-01-17T06:29:05.075912Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "8b7a4f24c209aa94",
   "outputs": [],
   "execution_count": 107
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
